package ngames.com.sushitv.api;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import ngames.com.sushitv.entity.Anime;
import ngames.com.sushitv.entity.ButtonHome;
import ngames.com.sushitv.entity.FanMadeContent;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.entity.ResetPasswordInfo;
import ngames.com.sushitv.entity.SearchPrompt;
import ngames.com.sushitv.entity.UpUser;
import ngames.com.sushitv.entity.UserInfo;
import ngames.com.sushitv.net.okhttp.OKHttpManage;
import ngames.com.sushitv.net.okhttp.callback.RequestCallBack;
import ngames.com.sushitv.net.okhttp.callback.RequestCallBackJSON;
import ngames.com.sushitv.tools.LogUtil;
import okhttp3.Response;

/**
 * Created by Jeffrey on 2016/7/26.
 */
public class ApiImplOKHttp implements Api {
    private OKHttpManage mOKHttpManage;

    public ApiImplOKHttp(Context context) {
        mOKHttpManage = OKHttpManage.getInstance(context);
    }

    @Override
    public void cancelRequest(String urlTag) {
        if (!TextUtils.isEmpty(urlTag)) {
            mOKHttpManage.cancelTag(urlTag);
        }
    }



    /**
     * POST /v1/user/login
     * 登录
     */
    @Override
    public void login(String type, String email, String area, String phone, String password, RequestCallBack<UserInfo> listener) {
        String registerUrl = URL_LOGIN;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>();
        params.put("type", type);
        params.put("email", email);
        params.put("area", area);
        params.put("phone", phone);
        params.put("password", password);
        mOKHttpManage.asynFormPost(registerUrl, params, listener);
    }

    /**
     * POST /v1/user/login-fb
     * 登录
     */
    @Override
    public void loginFB(String fbID, String email, RequestCallBack<UserInfo> listener) {
        String registerUrl = URL_LOGIN_FB;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>();
        params.put("fbid", fbID);
        params.put("email", email == null ? "" : email);
        LogUtil.d("OKHttpManage", "loginFB() fbid = " + fbID + ",email = " + email);
        mOKHttpManage.asynFormPost(registerUrl, params, listener);
    }

    /**
     * POST /v1/user/register
     */
    @Override
    public void register(String type, String fbid, String fbemail, String fbavatar, String name, String email, String password, RequestCallBack<UserInfo> listener) {
        String registerUrl = URL_REGISTER;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(17);
        params.put("type", type);
        params.put("fbid", fbid == null ? "" : fbid);
        params.put("fbemail", fbemail == null ? "" : fbemail);
        params.put("fbavatar", fbavatar == null ? "" : fbavatar);
        params.put("name", name);
        params.put("email", email);
        params.put("password", password);
        mOKHttpManage.asynFormPost(registerUrl, params, listener);
    }

    /**
     * POST /v1/user/register-phone
     */
    @Override
    public void registerPhone(String sms_key, String sms_code, String name, String password, String area, String phone, RequestCallBack<UserInfo> listener) {
        String registerUrl = URL_REGISTER_PHONE;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(17);
        params.put("sms_key", sms_key);
        params.put("sms_code", sms_code);
        params.put("name", name);
        params.put("password", password);
        params.put("area", area);
        params.put("phone", phone);
        mOKHttpManage.asynFormPost(registerUrl, params, listener);
    }

    /**
     * POST /v1/user/refresh-token
     * 令牌刷新
     */
    @Override
    public void refreshToken(String refreshToken, RequestCallBack<UserInfo> listener) {
        String registerUrl = URL_REFRESH_TOKEN;
        HashMap<String, String> params = new HashMap<>(8);
        params.put("refresh_token", refreshToken);
        mOKHttpManage.asynFormPost(registerUrl, params, listener);
    }

    /**
     * POST /v1/user/forgot
     * 忘记密码
     */
    @Override
    public void forgotPassword(String email, RequestCallBack<Void> listener) {
        String registerUrl = URL_FORGOT;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(8);
        params.put("email", email);
        mOKHttpManage.asynFormPost(registerUrl, params, listener);
    }

    /**
     * POST /v1/user/forgot-phone
     */
    @Override
    public void forgotPasswordPhone(String area, String phone, String sms_key, String sms_code, RequestCallBack<ResetPasswordInfo> listener) {
        String registerUrl = URL_FORGOT_PHONE;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(12);
        params.put("area", area);
        params.put("phone", phone);
        params.put("sms_key", sms_key);
        params.put("sms_code", sms_code);
        mOKHttpManage.asynFormPost(registerUrl, params, listener);
    }

    /**
     * POST /v1/user/reset
     * 重置密码
     */
    @Override
    public void resetPassword(String area, String phone, String resetToken, String password, RequestCallBack<Void> listener) {
        String registerUrl = URL_RESET_PASSWORD;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(12);
        params.put("area", area);
        params.put("phone", phone);
        params.put("resetToken", resetToken);
        params.put("password", password);
        mOKHttpManage.asynFormPost(registerUrl, params, listener);
    }

    /**
     * GET /v1/user/account-exists
     * 验证name、email是否被注册;type:email和name
     */
    @Override
    public void isRegistered(String type, String value, RequestCallBack<Void> listener) {
        String registerUrl = URL_VIDEO_RANK;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(10);
        params.put("field", type);
        params.put("value", value);
        mOKHttpManage.asynRequestGet(registerUrl, params, listener);
    }

    /**
     * POST /v1/user/send-sms
     * 发送验证码
     */
    @Override
    public void sendSms(String type, String phone, RequestCallBack<String> listener) {
        String registerUrl = URL_SEND_SMS;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(9);
        params.put("type", type);
        params.put("phone", phone);
        mOKHttpManage.asynFormPost(registerUrl, params, listener);
    }


//    /**
//     * 视频排行榜
//     */
//    @Override
//    public void videoRank(String code, String size, RequestCallBack<ArrayList<ItemVideo>> listener) {
//        String registerUrl = URL_VIDEO_RANK;
//        cancelRequest(registerUrl);
//        HashMap<String, String> params = new HashMap<>(2);
//        params.put("code", code);
//        params.put("size", size);
//        mOKHttpManage.asynRequestGet(registerUrl, params, listener);
//    }

    /**
     * 视频排行榜
     */
    @Override
    public void videoRank(String code, String size, RequestCallBack<ArrayList<ItemVideo>> listener) {
        String registerUrl = URL_VIDEO_RANK;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(2);
        params.put("code", code);
         params.put("size", size);
        mOKHttpManage.asynRequestGet(registerUrl, params, listener);
    }

    @Override
    public void videoRank(String id, RequestCallBackJSON<ArrayList<ItemVideo>> listener) {
        String registerUrl = URL_VIDEO_FANMADE_CONTENT;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(2);
        params.put("category_id", id);
        mOKHttpManage.asynRequestGet(registerUrl, params, listener);
    }

    @Override
    public void videoCates(RequestCallBack<ArrayList<ButtonHome>> listener) {
        String registerUrl = URL_VIDEO_CATES;
        cancelRequest(registerUrl);
        mOKHttpManage.asynRequestGet(registerUrl, null, listener);
    }

    @Override
    public void hotWord(RequestCallBack<ArrayList<String>> listener) {
        String registerUrl = URL_SEARCH_HOT_WORD;
        cancelRequest(registerUrl);
        mOKHttpManage.asynRequestGet(registerUrl, null, listener);
    }

    @Override
    public void searchPrompt(String word, String key, RequestCallBack<ArrayList<String>> listener) {
        String registerUrl = URL_SEARCH_PROMPT;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(2);
        params.put("word", word);
        mOKHttpManage.asynRequestGet(registerUrl, params, listener);
    }

    @Override
    public void searchRequest(String order,String type, String name, String page, RequestCallBack<ArrayList<Anime>> listener) {
        String registerUrl = URL_SEARCH;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(2);
        params.put("word",name);
        params.put("type",type);
        params.put("page",page);
        params.put("order",order);
        mOKHttpManage.asynRequestGet(registerUrl, params, listener);
    }

    @Override
    public void searchUserRequest(String type, String name, String page, RequestCallBackJSON<ArrayList<UpUser>> listener) {
        String registerUrl = URL_SEARCH;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(2);
        params.put("word",name);
        if(type.equals("users")){
            params.put("type","user");
        }else{
            params.put("type",type);
        }
        params.put("page",page);
        mOKHttpManage.asynRequestGet(registerUrl, params, listener);
    }

    @Override
    public void videoDetails(String vsId, RequestCallBack<ArrayList<ItemVideo>> listener) {
        String registerUrl = URL_VIDEO_DETAILS;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(2);
        params.put("vs_id",vsId);
        mOKHttpManage.asynRequestGet(registerUrl, params, listener);
    }

    @Override
    public void animeDetails(String vId, RequestCallBack<ArrayList<ItemVideo>> listener) {
        String registerUrl = URL_ANIME_DETAILS;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(2);
        params.put("v_id",vId);
        mOKHttpManage.asynRequestGet(registerUrl, params, listener);
    }

    @Override
    public void itemVideo(String id, int videoType, int total, int page, RequestCallBack<ArrayList<ItemVideo>> listener) {
        switch (videoType){
            case 1://用户收藏
                String registerUrl = URL_ITEMVIDEO;
                cancelRequest(registerUrl);
                HashMap<String, String> params = new HashMap<>(2);
                params.put("page",page+"");
                params.put("size",total+"");
                params.put("user_id","2016");
                mOKHttpManage.asynRequestGet(registerUrl, params, listener);
                break;
        }

    }

   /* @Override
    public void searchPrompt(String word, RequestCallBack<ArrayList<SearchPrompt>> listener) {
        String registerUrl = URL_SEARCH_PROMPT;
        cancelRequest(registerUrl);
        HashMap<String, String> params = new HashMap<>(2);
        params.put("word", word);
        mOKHttpManage.asynRequestGet(registerUrl, params, listener);
    }*/



   /* @Override
    public void videoContent(ArrayList<ButtonHome> data, RequestCallBack<ArrayList<FanMadeContent>> listener) {
        final ArrayList<FanMadeContent> faList=new ArrayList<>();

        String registerUrl = URL_VIDEO_FANMADE_CONTENT;
        cancelRequest(registerUrl);
        for(int i=0;i<data.size();++i) {
            final FanMadeContent f=new FanMadeContent();
            HashMap<String, String> params = new HashMap<>(2);
            final ButtonHome b=data.get(i);
            params.put("category_id", b.getId());
            RequestCallBack<ArrayList<ItemVideo>> itemListener = new RequestCallBack<ArrayList<ItemVideo>>() {
                @Override
                public ArrayList<ItemVideo> parseResponse(Response response) {
                    return null;
                }

                @Override
                public void onFailure(String code, String msg) {
                    Log.e("code&msg", code+"\n"+msg);
                }

                @Override
                public void onSuccess(ArrayList<ItemVideo> obj) {

                    f.setName(b.getName());
                    f.setItemVideos(obj);
                    faList.add(f);
                    Log.e("************", "------------------------------");
                }
            };
            mOKHttpManage.asynRequestGet(registerUrl, params, listener);
        }
        //mOKHttpManage.asynRequestGet(registerUrl, params, itemListener);
    }
*/

}
